class FormDataCheck {
  testID(ID) {
    return this.IDRegExp.test(String(ID));
  }
  testEmail(email) {
    return this.emailRegExp.test(String(email));
  }
  testPhoneNumber(phoneNumber) {
    return this.phoneNumberRegExp.test(String(phoneNumber));
  }
  //自定义方法
  // static add(checkFormItemName, checkFormRegExp) {
  static add(opt) {
    for (let checkFormItemName in opt) {
      let checkFormRegExp = RegExp(opt[checkFormItemName]);
      let newName = this.prototype.formDataHander(checkFormItemName);
      this.prototype[newName] = (checkData) => {
        return checkFormRegExp.test(String(checkData));
      };
    }
    //返回这个对象实例 链式调用
    return this;
  }
  static result(opt, check) {
    check && this.add(check);
    //没有需要自定义的就用默认的
    let res = opt ? true : false;
    let filture = [];
    for (let key in opt) {
      //这里的this是构造函数constructor
      let newkey = this.prototype.formDataHander(key);
      if (!this.prototype[newkey]) {
        return key + "'s RegExp is not fined";
      }
      res = res && this.prototype[newkey](opt[key]);
      filture.push(res);
    }
    return filture;
  }
  formDataHander(str) {
    return "test" + str[0].toUpperCase() + str.slice(1);
  }
  static check(opt, check) {
    return this.result(opt, check).every((value) => value);
  }
}
FormDataCheck.prototype.IDRegExp = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
FormDataCheck.prototype.phoneNumberRegExp = /^1[3456789]\d{9}$/;
FormDataCheck.prototype.emailRegExp = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;

FormDataCheck.add({ age: /11/ });
let res = FormDataCheck.check(
  {
    ID: "341225199908274916",
    name: 111,
    age: 111,
  },
  { name: 111 }
);
console.dir(FormDataCheck);
console.log(res);

export default FormDataCheck;
